// pages/countdown/countdown.js
var util = require('../../utils/util.js')
var http = require('../../utils/http.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    fiveTimes: false,
    countList: [
      // {
      //   peop: 101240,
      //   money: 1,
      //   img: '/discount/now_get.png'
      // },
      // {
      //   peop: 101240,
      //   money: 10,
      //   img: '/discount/go_on.png'
      // },
      // {
      //   peop: 101240,
      //   money: 20,
      //   img: '/discount/was_geted.png'
      // },
      // {
      //   peop: 101240,
      //   money: 100,
      //   img: '/discount/now_get.png'
      // },
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    http.discountList().then( (res) => {
      var countList = res.bargains;
      that.setData({
        countList
      })
    })
  },
  discount:function(e) {
    console.log(e);
    var that = this;
    var index = e.currentTarget.dataset.index;
    var countList = that.data.countList;
    var bargainId = countList[index].id;
    const data = {
      bargainId: bargainId
    }
    if( countList[index].type == 0) {
      //19 马上领取
    http.startBargain(data).then(res => {
      that.onShow();
    })
    } else if( countList[index].type == 1) {
      //继续砍价
      wx.navigateTo({
        url: '/pages/mycount/mycount?index='+ index + '&id='+ bargainId,
      })
    } else {
      //已经领取
    }


  },
  back:function () {
    wx.navigateBack({
      delta: 1000
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {


  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})